/**
 * 
 */
package edu.hadassah.store.policy;

import java.beans.PropertyChangeListener;

import edu.hadassah.store.employee.IEmployee;
import edu.hadassah.store.management.IStoreManagement;
import edu.hadassah.store.management.NotificationManager;
import edu.hadassah.store.order.IOrder;
import edu.hadassah.store.uniqueid.IUniqueId;

/**
 * @author persikov
 *
 */
public aspect EmployeeOperationPolicy {
	
	
	pointcut removeEmployee(IStoreManagement storeManagement, IUniqueId employeeId) : target(storeManagement) && args(employeeId) && call(boolean IStoreManagement+.removeEmployee(IUniqueId));

	/**
	 * @param storeManagement
	 * @param orderId
	 * @return
	 */
	boolean around (IStoreManagement storeManagement, IUniqueId employeeId):removeEmployee(storeManagement, employeeId){
		IEmployee employee = storeManagement.getEmployee(employeeId);
		Trace.traceEntry(thisJoinPoint);
		boolean removed =  proceed(storeManagement, employeeId);
		if (removed) {
			removeEntityHandler(storeManagement, employee);
		} 
		return removed;
	}

	/**
	 * @param storeManagement
	 * @param employee
	 */
	private void removeEntityHandler(IStoreManagement storeManagement, IEmployee employee) {
		for (IOrder order : storeManagement.getOrderRepository()) {
			if ( employee instanceof PropertyChangeListener){
				NotificationManager.getInstance().removeOrderListener(order.getId(), (PropertyChangeListener)employee);
			}
		}
	}

}
